package com.geekteam.manga.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

import com.geekteam.manga.provider.MangaContract.ContentMangaColumns;
import com.geekteam.manga.provider.MangaContract.MangaChapColumns;
import com.geekteam.manga.provider.MangaContract.MangaColumns;
import com.geekteam.manga.provider.MangaContract.RecentMangaColumns;

public class MangaDatabase extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "manga.db";
	private static final int VER_LAUNCH = 0;
	private static final int VER_SESSION_TYPE = 6;
	private static final int DATABASE_VERSION = VER_SESSION_TYPE;

	interface Tables {
		String MANGA = "manga";
		String MANGACHAP = "mangachap";
		String CONTENTMANGA = "contentmanga";
		String RECENTMANGA = "recentmanga";
	}

	public MangaDatabase(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public MangaDatabase(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		db.execSQL("CREATE TABLE " + Tables.MANGA + "(" + BaseColumns._ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT," + MangaColumns.MANGA_ID
				+ " TEXT NOT NULL," + MangaColumns.MANGA_TITLE + " TEXT,"
				+ MangaColumns.MANGA_LIKENUMBER + " INTEGER,"
				+ MangaColumns.MANGA_VIEWNUMBER + " INTEGER,"
				+ MangaColumns.MANGA_URLCOVER + " TEXT,"
				+ MangaColumns.MANGA_DESCRIPTION + " TEXT,"
				+ MangaColumns.MANGA_AUTHOR + " TEXT,"
				+ MangaColumns.MANGA_SOURCE + " TEXT,"
				+ MangaColumns.MANGA_FAVORITE + " INTEGER," + "UNIQUE ("
				+ MangaColumns.MANGA_ID + ") ON CONFLICT REPLACE)");

		db.execSQL("CREATE TABLE " + Tables.MANGACHAP + "(" + BaseColumns._ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ MangaChapColumns.MANGA_ID + " TEXT,"
				+ MangaChapColumns.MANGACHAP_CHAPID + " TEXT,"
				+ MangaChapColumns.MANGACHAP_NAME + " TEXT,"
				+ MangaChapColumns.MANGACHAP_NUMBER + " INTEGER," + " UNIQUE("
				+ MangaChapColumns.MANGACHAP_CHAPID + ") ON CONFLICT REPLACE)");

		db.execSQL("CREATE TABLE " + Tables.CONTENTMANGA + "("
				+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ ContentMangaColumns.MANGA_ID + " TEXT NOT NULL,"
				+ ContentMangaColumns.MANGA_CHAPID + " TEXT,"
				+ ContentMangaColumns.MANGA_CONTENT + " TEXT,"
				+ "UNIQUE(" + ContentMangaColumns.MANGA_CHAPID
				+ ") ON CONFLICT REPLACE)");

		db.execSQL("CREATE TABLE " + Tables.RECENTMANGA + "(" + BaseColumns._ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ RecentMangaColumns.MANGA_ID + " TEXT,"
				+ RecentMangaColumns.RECENTMANGA_TIME + " TEXT," + "UNIQUE("
				+ RecentMangaColumns.MANGA_ID + ") ON CONFLICT REPLACE)");
		insertManga(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		int version = oldVersion;
		switch (version) {
		case VER_LAUNCH:
			version = VER_SESSION_TYPE;
		}
		if (version != DATABASE_VERSION) {
			db.execSQL("DROP TABLE IF EXISTS " + Tables.MANGA);
			db.execSQL("DROP TABLE IF EXISTS " + Tables.MANGACHAP);
			db.execSQL("DROP TABLE IF EXISTS " + Tables.CONTENTMANGA);
			db.execSQL("DROP TABLE IF EXISTS " + Tables.RECENTMANGA);
			onCreate(db);
		}
	}

	public static void deleteDatabase(Context context) {
		context.deleteDatabase(DATABASE_NAME);
	}

	private void insertManga(SQLiteDatabase db) {
		db.execSQL("INSERT INTO "
				+ Tables.MANGA
				+ "("
				+ MangaColumns.MANGA_ID
				+ ", "
				+ MangaColumns.MANGA_TITLE
				+ ", "
				+ MangaColumns.MANGA_LIKENUMBER
				+ ", "
				+ MangaColumns.MANGA_VIEWNUMBER
				+ ", "
				+ MangaColumns.MANGA_URLCOVER
				+ ", "
				+ MangaColumns.MANGA_DESCRIPTION
				+ ", "
				+ MangaColumns.MANGA_AUTHOR
				+ ", "
				+ MangaColumns.MANGA_SOURCE
				+ ", "
				+ MangaColumns.MANGA_FAVORITE
				+ ") VALUES ('TTLHM', 'Tân Tác Long Hổ Môn', 20, 30,'fdfdfd','Hay lắm','ko biet','mangaviet.com',1)");

		db.execSQL("INSERT INTO "
				+ Tables.MANGA
				+ "("
				+ MangaColumns.MANGA_ID
				+ ", "
				+ MangaColumns.MANGA_TITLE
				+ ", "
				+ MangaColumns.MANGA_LIKENUMBER
				+ ", "
				+ MangaColumns.MANGA_VIEWNUMBER
				+ ", "
				+ MangaColumns.MANGA_URLCOVER
				+ ", "
				+ MangaColumns.MANGA_DESCRIPTION
				+ ", "
				+ MangaColumns.MANGA_AUTHOR
				+ ", "
				+ MangaColumns.MANGA_SOURCE
				+ ", "
				+ MangaColumns.MANGA_FAVORITE
				+ ") VALUES ('PV', 'Phong Van', 30, 30,'fdrrerfd','Hay lắm','ko biet','mangaviet.com',1)");
	}
}
